//
//  QuestionsViewController.m
//  CulinaryKnowledge
//
//  Created by CaoHung on 7/16/14.
//  Copyright (c) 2014 GreenSun. All rights reserved.
//

#import "QuestionsViewController.h"
#import "AnswerViewController.h"

#define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
#define IS_OS_7    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

@interface QuestionsViewController ()

@end

@implementation QuestionsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Layout screen
    [self layoutScreen];
    mIndex = 1;
    mPoints = 0;
    
    /* Save result
    if (self.saveIndex == 0) {
        mIndex = 1;
        mPoints = 0;
    }else{
        mIndex = self.saveIndex;
        mPoints = self.savePoints;
    }
     */
    
    quesionsList = self.mQuesionsList;
    [self loadQuestion];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(nextQuestion)
                                                 name:@"nextQuestion"
                                               object:nil];
}

// Layout screen
- (void)layoutScreen
{
    self.btnCorrect.userInteractionEnabled = NO;
    self.btnInCorrect.userInteractionEnabled = NO;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Question.png"]];
    
    if (isiPhone5) {
        self.backgroundQuestions.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Questions4inch.png"]];
        if (IS_OS_7) {
            self.backgroundQuestions.frame = CGRectMake(15.0f, 101.0f, 291.0f, 457.5f);
        }
        else{
            self.contentQuestionView.frame = CGRectMake(15.0f, 60.0f, 285.0f, 193.0f);
            self.tietleQuestionView.frame = CGRectMake(15.0f, 10.0f, 291.0f, 30.0f);
            self.backgroundQuestions.frame = CGRectMake(15.0f, 40.0f, 291.0f, 457.5f);
        }
    }
    else{
        self.backgroundQuestions.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Questions3.5inch.png"]];

        if (IS_OS_7) {
            
            self.contentQuestionView.frame = CGRectMake(15.0f, 110.0f, 285.0f, 193.0f);
            
            self.backgroundQuestions.frame = CGRectMake(15.0f, 101.0f, 291.0f, 369.0f);
            //Set frame for button answer
            CGRect frameAnswer = self.btnAnswer1.frame;

            frameAnswer = CGRectMake(frameAnswer.origin.x, 212.0f, frameAnswer.size.width, frameAnswer.size.height);
            self.btnAnswer1.frame = frameAnswer;
            
            frameAnswer = CGRectMake(frameAnswer.origin.x, 212.0f + 48.0f, frameAnswer.size.width, frameAnswer.size.height);
            self.btnAnswer2.frame = frameAnswer;
            
            frameAnswer = CGRectMake(frameAnswer.origin.x, 212.0f + 96.0f, frameAnswer.size.width, frameAnswer.size.height);
            self.btnAnswer3.frame = frameAnswer;
        }
        else{
            
            self.contentQuestionView.frame = CGRectMake(15.0f, 50.0f, 285.0f, 193.0f);
            self.backgroundQuestions.frame = CGRectMake(15.0f, 40.0f, 291.0f, 369.0f);
            self.tietleQuestionView.frame = CGRectMake(15.0f, 10.0f, 291.0f, 30.0f);
            //Set frame for button answer
            CGRect frameAnswer = self.btnAnswer1.frame;
            
            frameAnswer = CGRectMake(frameAnswer.origin.x, 212.0f, frameAnswer.size.width, frameAnswer.size.height);
            self.btnAnswer1.frame = frameAnswer;
            
            frameAnswer = CGRectMake(frameAnswer.origin.x, 212.0f + 48.0f, frameAnswer.size.width, frameAnswer.size.height);
            self.btnAnswer2.frame = frameAnswer;
            
            frameAnswer = CGRectMake(frameAnswer.origin.x, 212.0f + 96.0f, frameAnswer.size.width, frameAnswer.size.height);
            self.btnAnswer3.frame = frameAnswer;
        }
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    self.title = @"うなぎ検定";
    //Show navigationController
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    /* Save result
    NSArray* views = [self.navigationController viewControllers];
    if ([views count] == 1) {
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
        [dictionary setObject:[NSString stringWithFormat:@"%ld", (long)mIndex] forKey:@"index"];
        [dictionary setObject:[NSString stringWithFormat:@"%ld", (long)mPoints] forKey:@"points"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"saveResult" object:dictionary];
    }
     */
}

// Load Question
- (void)loadQuestion
{
    self.tietleQuestionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"Image_title_question%ld.png", (long)mIndex]]];
    self.contentQuestionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"content_Question%ld.png", (long)mIndex]]];
    NSString *keyString = [NSString stringWithFormat:@"Question%ld", (long)mIndex];
    NSDictionary *dictionary = [quesionsList objectForKey:keyString];
    mQuestion = [dictionary objectForKey:@"question"];
    mAnswers = [dictionary objectForKey:@"answers"];
    mResult = [dictionary objectForKey:@"result"];
    
    self.lbQuestion.text = mQuestion;
    
    if ([mAnswers count] == 3) {
        [self.btnAnswer1 setTitle:[NSString stringWithFormat:@"        %@", [mAnswers objectAtIndex:0]]forState:UIControlStateNormal];
        [self.btnAnswer2 setTitle:[NSString stringWithFormat:@"        %@", [mAnswers objectAtIndex:1]]forState:UIControlStateNormal];
        self.btnAnswer3.alpha = 1;
        [self.btnAnswer3 setTitle:[NSString stringWithFormat:@"        %@", [mAnswers objectAtIndex:2]]forState:UIControlStateNormal];
    }
    else{
        [self.btnAnswer1 setTitle:[NSString stringWithFormat:@"        %@", [mAnswers objectAtIndex:0]]forState:UIControlStateNormal];
        [self.btnAnswer2 setTitle:[NSString stringWithFormat:@"        %@", [mAnswers objectAtIndex:1]]forState:UIControlStateNormal];
        self.btnAnswer3.alpha = 0;
    }
}

// Next Question
- (void)nextQuestion
{
    isSelectAnswer = NO;
    mIndex ++;
    self.btnCorrect.alpha = 0;
    self.btnInCorrect.alpha = 0;
    [self loadQuestion];
}

// Cick choose answer 1
- (IBAction)actionAnswer1:(id)sender {
    
    if(isSelectAnswer)
    {
        return;
    }
    
    isSelectAnswer = YES;
    if ([mResult isEqualToString:@"1"]) {
        mPoints ++;
        [self gotoAnswerDetail:YES];
    }
    else{
        [self gotoAnswerDetail:NO];
    }
}

// Cick choose answer 2
- (IBAction)actionAnswer2:(id)sender {
    
    if(isSelectAnswer)
    {
        return;
    }
    
    isSelectAnswer = YES;
    if ([mResult isEqualToString:@"2"]) {
        mPoints ++;
        [self gotoAnswerDetail:YES];
    }
    else{
        [self gotoAnswerDetail:NO];
    }
}

// Cick choose answer 3
- (IBAction)actionAnswer3:(id)sender {
    
    if(isSelectAnswer)
    {
        return;
    }
    
    isSelectAnswer = YES;
    if ([mResult isEqualToString:@"3"]) {
        mPoints ++;
        [self gotoAnswerDetail:YES];
    }
    else{
        [self gotoAnswerDetail:NO];
    }
}

// Go to answer detail
- (void)gotoAnswerDetail:(BOOL)result
{
    if (!IS_OS_7) {
        if(isiPhone5){
            CGRect frameButton = CGRectMake(0.0f, -33.0f, 320.0f, 568.0f);
            self.btnCorrect.frame = frameButton;
            self.btnInCorrect.frame = frameButton;
        }
        else{
            CGRect frameButton = CGRectMake(0.0f, -55.0f, 320.0f, 542.0f);
            self.btnCorrect.frame = frameButton;
            self.btnInCorrect.frame = frameButton;
        }
    }
    else{
        if(isiPhone5){
            CGRect frameButton = CGRectMake(0.0f, 33.0f, 320.0f, 568.0f);
            self.btnCorrect.frame = frameButton;
            self.btnInCorrect.frame = frameButton;
        }
    }
    
    if (result) {
        [UIView animateWithDuration:0.5
                              delay:0.3
                            options: UIViewAnimationCurveEaseOut
                         animations:^{
                             self.btnCorrect.alpha = 1.0;
                         }
                         completion:^(BOOL finished){
                             AnswerViewController *viewContronller = [[AnswerViewController alloc]init];
                             [viewContronller setMQuestion:mQuestion];
                             [viewContronller setMAnswers:mAnswers];
                             [viewContronller setMResult:mResult];
                             [viewContronller setMIndex:mIndex];
                             [viewContronller setMPoints:mPoints];
                             
                             self.title = @"タイトル";
                             [self.navigationController pushViewController:viewContronller animated:YES];
                         }];

    }
    else{
        [UIView animateWithDuration:0.5
                              delay:0.3
                            options: UIViewAnimationCurveEaseOut
                         animations:^{
                             self.btnInCorrect.alpha = 1.0;
                         }
                         completion:^(BOOL finished){
                             AnswerViewController *viewContronller = [[AnswerViewController alloc]init];
                             [viewContronller setMQuestion:mQuestion];
                             [viewContronller setMAnswers:mAnswers];
                             [viewContronller setMResult:mResult];
                             [viewContronller setMIndex:mIndex];
                             [viewContronller setMPoints:mPoints];
                             
                             self.title = @"タイトル";
                             [self.navigationController pushViewController:viewContronller animated:YES];
                         }];
    }
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
